package shop

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"
	"fast-boot/common/globalkey"
	"github.com/jinzhu/copier"

	"github.com/zeromicro/go-zero/core/logx"
)

type CategoryAttributeListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 新增类型-属性
func NewCategoryAttributeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CategoryAttributeListLogic {
	return &CategoryAttributeListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CategoryAttributeListLogic) CategoryAttributeList(req *types.PmsCategoryAttributeListReq) (resp []*types.PmsCategoryAttribute, err error) {
	m := model.PmsCategoryAttributeModel{}
	items := make([]*model.PmsCategoryAttributeModel, 0)
	l.svcCtx.GormClient.GormDb.Model(m).
		Where("type = ?", req.Type).
		Where("category_id = ?", req.CategoryId).
		Order("id asc").Find(&items)

	list := make([]*types.PmsCategoryAttribute, 0)

	if err := copier.CopyWithOption(&list, items, globalkey.CopierProtoOptions); err != nil {
		return nil, err
	}

	return list, nil
}
